<?php
/*
 * @分销商城
 * @功能说明：服务订单主体
 * @更新说明：暂无更新
 * @文件名 ServiceController.class.php
 * @编码 UTF-8
 * @创建时间 2016-1-24 下午4:16:10
 * @创建人 XING
 */

namespace api\Controller;
class ServiceController extends ApibaseController
{
	/*
	 * 分销商城_服务主页
	 * @date: 2016-1-25 下午12:15:07
	 * @editor: XING
	 */
	public function digService(){
		if(I('post.service_id') && I('post.cust_id')){
			$storyEntity = M('ServiceEntity')
						 ->where(array('service_id'=>I('post.service_id')))->find();
			if(!$storyEntity){
				dataFormat('',0,'没有该服务');
			}
			switch(I('post.service_id')){
				case '1':
					break;
				case '2':
					$is_on = M('SstoryEntity')
						   ->where('cust_id = '.I('post.cust_id').' AND end_at > '.time())->count();
					dataFormat(array('service_id' => '2','price' => $storyEntity['price'] > 0 ? $storyEntity['price'] : 0,'isDy' => $is_on > 0 ? 1 : 0),'1','success');
					break;
				case '3':
					$is_on = M('SclockEntity')->where('end_at > '.time())
						   ->where(array('cust_id' => I('post.cust_id')))->count();
					dataFormat(array('service_id' => '3','price' => 0,'isDy' => $is_on > 0 ? 1 : 0),'1','success');
						
					break;
				default:
					break;
			}
		}else{
			dataFormat('',0,'参数有误');
		}
	}
	
	/*
	 * 分销商城_讲故事订阅
	 * @date: 2016-1-25 下午3:53:37
	 * @editor: XING
	 */
	public function readStory(){
		if(I('post.cust_id')){
			$checkExist = M('SstoryEntity')
					    ->where('cust_id = '.I('post.cust_id').' AND end_at > '.time())->count();
			if(!$checkExist){
				$checkPrice = M('ServiceEntity')->getFieldByServiceId('2','price');
				if($checkPrice > 0){
					$getOrderProduct = date('YHd').rand(10,99).date('sim').rand(100,999);
					$userMsg = M('CustEntity')
							 ->where(array('cust_id'=>I('post.cust_id')))->find();
					$userCollect = M('OrderCollect');
					$userCollect ->startTrans();
					$addCollect = $userCollect->lock(true)->add(array(
							'order_id' => $getOrderProduct,
							'biz_id' => '0',
							'biz_title' => '订阅服务',
							'goods_title' => '订阅讲故事服务费',
							'goods_pic' => '',
							'pay_price' => trim($checkPrice),
							'pay_nums' => '1',
							'add_at' => time(),
							'update_at' => time()
					));
					if($addCollect){
						$pushOrder = M('OrderEntity');
						$setEffect = $pushOrder->lock(true)->add(array(
								'order_id' => $getOrderProduct,
								'order_amount' => $checkPrice,
								'order_pay' => $checkPrice,
								'order_status' => '7',
								'otype' => '1',
								'cust_id' => I('post.cust_id'),
								'cust_mobile' => $userMsg['cust_mobile'],
								'cust_realname' => $userMsg['cust_realname'],
								'remark' => '2_0',
								'maccode' => '',
								'add_at' => time(),
								'udpate_at' => time()
						));
						if($setEffect){
							$pushOrder->commit();
							$userCollect->commit();
							dataFormat($getOrderProduct,1,'success');
						}else{
							$pushOrder->rollback();
							$userCollect->rollback();
							dataFormat('',0,'事务流出错');
						}
					}else{
						$userCollect->rollback();
						dataFormat('',0,'事务流出错');
					}
				}else{
					$getMacCode = M('HwcodeEntity')->field('maccode')
								->where(array('cust_id'=>I('post.cust_id')))->select();
					if($getMacCode){
						foreach($getMacCode AS $val){
							$putIn = M('SstoryEntity')->add(array(
										'maccode' => $val['maccode'],
										'cust_id' => I('post.cust_id'),
										'is_open' => '1',
										'end_at' => time()+30*24*3600,
										'add_at' => time(),
										'update_at' => time()
									));
							if($putIn){
								dataFormat('',1,'订阅成功');
							}else{
								dataFormat('',0,'参数有误');
							}
						}
					}else{
						dataFormat('',0,'还没绑定魔盒硬件');
					}
				}
				
			}else{
				dataFormat('',0,'您已经订阅过了');
			}
		}else{
			dataFormat('',0,'参数有误');
		}
	}
	
	/*
	 * 分销商城_闹钟服务详情
	 * @date: 2016-1-25 下午8:16:33
	 * @editor: XING
	 */
	public function clockService(){
		$getClockMsg = M('ServiceClock')
					 ->order(array('add_at'=>'desc'))->select();
		if($getClockMsg){
			$getClocks = M('SclockEntity')
					   ->field('clock_id')->where('cust_id = '.I('post.cust_id').' AND end_at > '.time())->select();
			$clocks = array();
			if($getClocks){
				foreach($getClocks AS $val){
					$clocks[] = $val['clock_id'];
				}
			}
			foreach($getClockMsg AS $val){
				$output[] = array(
						'clock_id' => $val['clock_id'],
						'clock_title' => $val['clock_title'],
						'clock_pic' => transUrl($val['clock_pic']),
						'clock_url' => transUrl($val['clock_url']),
						'price' => $val['price'],
						'is_on' => in_array(intval($val['clock_id']),$clocks) ? 1 : 0	
					);
			}
			
			dataFormat($output);
		}else{
			dataFormat('',1,'没有闹钟数据');
		}
	}
	
	/*
	 * 分销商城_订阅闹钟
	 * @date: 2016-1-26 下午3:20:25
	 * @editor: XING
	 */
	public function digClocks(){
		if(I('post.cust_id') && I('post.clock_id')){
			$getPrice = M('ServiceClock')->getFieldByClockId(I('post.clock_id'),'price');
			$checkExist = M('SclockEntity')
						->where(array('cust_id'=>I('post.cust_id'),'clock_id'=>I('post.clock_id')))->find();
			if(!empty($checkExist) && $checkExist['end_at'] > time()){
				dataFormat('',0,'您已经订阅过了');
			}else if($checkExist['end_at'] <= time()){
				M('SclockEntity')
					->where('cust_id = '.I('post.cust_id').' AND clock_id = '.I('post.clock_id'))->delete();
				M('SclockDesc')
					->where('cust_id = '.I('post.cust_id').' AND clock_id = '.I('post.clock_id'))->delete();
			}
			if($getPrice > 0){
				$getOrderProduct = date('YHd').rand(10,99).date('sim').rand(100,999);
				$userMsg = M('CustEntity')
						 ->where(array('cust_id'=>I('post.cust_id')))->find();
				$userCollect = M('OrderCollect');
				$userCollect ->startTrans();
				$addCollect = $userCollect->lock(true)->add(array(
						'order_id' => $getOrderProduct,
						'biz_id' => '0',
						'biz_title' => '订阅服务',
						'goods_title' => '订阅闹钟服务费',
						'goods_pic' => '',
						'pay_price' => trim($getPrice),
						'pay_nums' => '1',
						'add_at' => time(),
						'update_at' => time()
				));
				if($addCollect){
					$pushOrder = M('OrderEntity');
					$setEffect = $pushOrder->lock(true)->add(array(
							'order_id' => $getOrderProduct,
							'order_amount' => $getPrice,
							'order_pay' => $getPrice,
							'order_status' => '7',
							'otype' => '1',
							'cust_id' => I('post.cust_id'),
							'cust_mobile' => $userMsg['cust_mobile'],
							'cust_realname' => $userMsg['cust_realname'],
							'remark' => '3_'.I('post.clock_id'),
							'maccode' => '',
							'add_at' => time(),
							'udpate_at' => time()
					));
					if($setEffect){
						$pushOrder->commit();
						$userCollect->commit();
						dataFormat($getOrderProduct,1,'success');
					}else{
						$pushOrder->rollback();
						$userCollect->rollback();
						dataFormat('',0,'事务流出错');
					}
				}else{
					$userCollect->rollback();
					dataFormat('',0,'事务流出错');
				}
			}else{
				$getMacCode = M('HwcodeEntity')->field('maccode')
							->where(array('cust_id'=>I('post.cust_id')))->select();
				if($getMacCode){
					foreach($getMacCode AS $val){
						if(empty($checkExist)){
							$putIn = M('SclockEntity')->add(array(	
									'clock_id' => I('post.clock_id'),
									'maccode' => $val['maccode'],
									'cust_id' => I('post.cust_id'),
									'is_open' => '1',
									'end_at' => time()+30*24*3600,
									'add_at' => time(),
									'update_at' => time()
							));
						}else{
							$putIn = M('SclockEntity')->where('clock_id = '.I('post.clock_id').' AND maccode = '.$val['maccode'].' AND cust_id = '.I('post.cust_id'))->setField(array(
									'is_open' => '1',
									'end_at' => time()+30*24*3600,
									'update_at' => time()
							));
							if($putIn){
								M('SclockDesc')->where('clock_id = '.I('post.clock_id').' AND maccode = '.$val['maccode'].' AND cust_id = '.I('post.cust_id'))->setField(array(
									'end_at' => time()+30*24*3600,
									'update_at' => time()
								));
							}
						}
						
					}
					if($putIn){
						dataFormat('',1,'订阅成功');
					}else{
						dataFormat('',0,'参数有误');
					}
				}else{
					dataFormat('',0,'还没绑定魔盒硬件');
				}
			}
		}else{
			dataFormat('',0,'参数有误');
		}
	}
	
	/*
	 * 分销商城_我的订阅
	 * @date: 2016-1-26 下午5:07:50
	 * @editor: XING
	 */
	public function myService(){
		if(I('post.cust_id')){
			$output = array();
			$storyIs = M('SstoryEntity')->field('end_at')
					 ->where('cust_id = '.I('post.cust_id').' AND end_at > '.time())->find();
			if($storyIs){
				$output[] = array(
							'service_id' => '2',
							'title' => '睡前故事',
							'id' => '0',
							'end_at' => date('Y-m-d H:i',$storyIs['end_at'])
						);
			}
			$sql = 'SELECT pdl_service_clock.*,pdl_sclock_entity.end_at,pdl_sclock_entity.clock_id FROM pdl_service_clock 
					INNER JOIN pdl_sclock_entity 
					ON pdl_sclock_entity.clock_id = pdl_service_clock.clock_id 
					WHERE cust_id = '.I('post.cust_id').' AND end_at > '.time();
			$queryData = CQuery($sql);
			if($queryData){
				foreach($queryData AS $vv){
					$output[] = array(
							'service_id' => '3',
							'id' => $vv['clock_id'],
							'title' => '闹钟服务',
							'clock_title' => $vv['clock_title'],
							'clock_pic' => transUrl($vv['clock_pic']),
							'end_at' => date('Y-m-d H:i',$vv['end_at'])
						);
				}
			}
			
			dataFormat($output);
		}else{
			dataFormat('',0,'参数有误');
		}
	}
	
	/*
	 * 分销商城_订阅了的闹钟选择
	 * @date: 2016-1-27 下午2:06:08
	 * @editor: XING
	 */
	public function caseClock(){
		if(I('post.cust_id')){
			$output = array();
			$sql = 'SELECT pdl_service_clock.*,pdl_sclock_entity.end_at,pdl_sclock_entity.clock_id FROM pdl_service_clock
					INNER JOIN pdl_sclock_entity
					ON pdl_sclock_entity.clock_id = pdl_service_clock.clock_id
					WHERE cust_id = '.I('post.cust_id').' AND end_at > '.time();
			$queryData = CQuery($sql);
			if($queryData){
				foreach($queryData AS $vv){
					$output[] = array(
							'clock_id' => $vv['clock_id'],
							'clock_title' => $vv['clock_title'],
					);
				}
				dataFormat($output);
			}else{
				dataFormat('',0,'没有订阅');
			}
		}else{
			dataFormat('',0,'参数有误');
		}
	}
	
	/*
	 * 分销商城_取消订阅
	 * @date: 2016-1-26 下午5:37:08
	 * @editor: XING
	 */
	public function cleService(){
		if(I('post.cust_id') && I('post.service_id') && isset($_POST['id'])){
			switch(I('post.service_id')){
				case '2':
					M('SstoryEntity')
							->where('cust_id = '.I('post.cust_id'))->delete();
					break;
				case '3':
					if(!I('post.id')){
						dataFormat('',0,'参数有误');
					}
					M('SclockEntity')
						->where('cust_id = '.I('post.cust_id').' AND clock_id = '.I('post.id'))->delete();
					M('SclockDesc')
						->where('cust_id = '.I('post.cust_id').' AND clock_id = '.I('post.id'))->delete();
					break;
				default:
					break;
			}
			
			dataFormat('',1,'取消订阅成功');
		}else{
			dataFormat('',0,'参数有误');
		}
	}

	/*
	 * 分销商城_设置闹钟
	 * @date: 2016-1-26 下午7:35:55
	 * @editor: XING
	 */
	public function setClock(){
		if(I('post.cust_id') && I('post.clock_id') && I('post.week') && isset($_POST['hour']) && isset($_POST['minutes'])){
			$isLegal = M('SclockEntity')->field('end_at')
					 ->where(array('cust_id'=>I('post.cust_id'),'clock_id'=>I('post.clock_id')))->where('end_at > '.time())->find();
			if(empty($isLegal)){
				dataFormat('',0,'请先订阅该闹钟');
			}
			$getMacCode = M('HwcodeEntity')->field('maccode')
						->where(array('cust_id'=>I('post.cust_id')))->select();
			if($getMacCode){
				$getMaxPici = M('SclockDesc')->max('pici');
				foreach($getMacCode AS $val){
					$putClock = M('SclockDesc')->add(array(
							  		'cust_id' => I('post.cust_id'),
							  		'clock_id' => I('post.clock_id'),
									'maccode' => $val['maccode'],
									'pici' => $getMaxPici+1,
									'clock_hour' => I('post.hour'),
									'clock_minute' => I('post.minutes'),
									'week_day' => I('post.week'),
									'is_open' => '1',
									'end_at' => $isLegal['end_at'],
									'add_at' => time(),
									'update_at' => time()
							));
						
				}
				if($putClock){
					dataFormat('',1,'设置成功');
				}else{
					dataFormat('',0,'操作失败');
				}
			}else{
				dataFormat('',0,'还没绑定魔盒硬件');
			}
		}else{
			dataFormat('',0,'参数有误');
		}
		
	}

	/*
	 * 分销商城_闹钟列表
	 * @date: 2016-1-27 上午10:46:59
	 * @id:均为批次id
	 * @editor: XING
	 */
	public function clockList(){
		if(I('post.cust_id')){
			$getClockList = M('SclockDesc')
					      ->where('cust_id = '.I('post.cust_id').' AND clock_id = '.I('post.clock_id'))->group('pici')->select();
			if($getClockList){
				$weekName = array(
							'','星期一','星期二','星期三','星期四','星期五','星期六','星期日'
						);
				$userWeek = array();$output = array();
				foreach($getClockList AS $val){
					if($val['week_day']){
						foreach(explode(',',$val['week_day']) AS $vv){
							$userWeek[] = $weekName[$vv];
						}
					}
					$output[] = array(
							'hour' => $val['clock_hour'],
							'minutes' => $val['clock_minute'],
							'week' => implode(',',$userWeek),
							'is_open' => $val['is_open'],
							'id' => $val['pici']
						);	
					$userWeek = array();
				}
				dataFormat($output,1,'success');
			}else{
				dataFormat('',1,'尚未设置闹钟');
			}
		}else{
			dataFormat('',0,'参数有误');
		}
	}
	
	/*
	 * 分销商城_闹钟删除
	 * @date: 2016-1-27 上午11:31:28
	 * @id:均为批次id
	 * @editor: XING
	 */
	public function delClock(){
		if(I('post.cust_id') && I('post.id')){
			$putDelete = M('SclockDesc')
					   ->where(array('cust_id' => I('post.cust_id'),'pici' => I('post.id')))->delete();
			if($putDelete){
				dataFormat('',1,'删除成功');
			}else{
				dataFormat('',0,'操作失败');
			}
		}else{
			dataFormat('',0,'参数有误');
		}
	}
	
	/*
	 * 分销商城_闹钟开关设置
	 * @date: 2016-1-27 上午11:59:17
	 * @id:均为批次id
	 * @editor: XING
	 */
	public function openClock(){
		if(I('post.cust_id') && I('post.id')){
			$sClock = M('SclockDesc')
					->where(array('pici' => I('post.id'),'cust_id' => I('post.cust_id')))->find();
			if($sClock){
				$putEffect = M('SclockDesc')
						   ->where(array('pici' => I('post.id'),'cust_id' => I('post.cust_id')))->setField(array(
						   			'is_open' => $sClock['is_open'] > 0 ? 0 : 1,
						   			'update_at' => time()
						   		));
				if($putEffect){
					dataFormat('',1,'闹钟已'.($sClock['is_open'] > 0 ? '关闭' : '开启'));
				}else{
					dataFormat('',0,'操作失败');
				}
			}else{
				dataFormat('',0,'非法参数');
			}
		}else{
			dataFormat('',0,'参数有误');
		}
	}
	
	/*
	 * 分销商城_朵拉故事开关
	 * @date: 2016-1-27 下午2:52:13
	 * @editor: XING
	 */
	public function openStory(){
		if(I('post.cust_id')){
			$sClock = M('SstoryEntity')
					->where(array('cust_id' => I('post.cust_id')))->find();
			if($sClock){
				$putEffect = M('SstoryEntity')
					->where(array('cust_id' => I('post.cust_id')))->setField(array(
							'is_open' => $sClock['is_open'] > 0 ? 0 : 1,
							'update_at' => time()
					));
				if($putEffect){
					dataFormat('',1,'设置已'.($sClock['is_open'] > 0 ? '关闭' : '开启'));
				}else{
					dataFormat('',0,'操作失败');
				}
			}else{
				dataFormat('',0,'非法参数');
			}
		}else{
			dataFormat('',0,'参数有误');
		}
	}
	
	/*
	 * 分销商城_开关数据获取
	 * @date: 2016-1-27 下午3:00:59
	 * @editor: XING
	 */
	public function data(){
		if(I('post.cust_id')){
			$sClock = M('SstoryEntity')->field('is_open')
					->where(array('cust_id' => I('post.cust_id')))->find();
			dataFormat($sClock['is_open']);
		}else{
			dataFormat('',0,'参数有误');
		}
	}
}